/**
 *It is a demo for practicing JAVA. All codes are opened source by Jian.
 */
package jian.tool.daocode.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tracy
 * @date 2012-11-14
 * @version
 * 
 */
public class Util {

	public static String ENTITY_PACKAGE = "tungsten.t.database.entity";
	public static String DAO_PACKAGE = "tungsten.t.database.dao";
	public static String DAOIMPL_PACKAGE = "tungsten.t.database.dao";
	public static String BLL_PACKAGE = "tungsten.t.database..bll";

	public static final Map<String, String> JAVA_TYPES = new HashMap<String, String>();

	public static final Map<String, String> SQL_TYPES = new HashMap<String, String>();

	static {
		JAVA_TYPES.put("java.lang.String", "String");
		JAVA_TYPES.put("java.lang.Byte", "byte");
		JAVA_TYPES.put("java.lang.Short", "short");
		JAVA_TYPES.put("java.lang.Integer", "int");
		JAVA_TYPES.put("java.lang.Long", "long");
		JAVA_TYPES.put("java.lang.Float", "float");
		JAVA_TYPES.put("java.lang.Double", "double");
		JAVA_TYPES.put("java.lang.Boolean", "boolean");

		SQL_TYPES.put("INT", "int");
		SQL_TYPES.put("TINYINT", "byte");
		SQL_TYPES.put("VARCHAR", "String");
		SQL_TYPES.put("FLOAT", "float");
		SQL_TYPES.put("TIMESTAP", "Date");
		SQL_TYPES.put("BIGINT", "long");
		SQL_TYPES.put("SMALLINT", "short");
		SQL_TYPES.put("DATETIME", "Date");
		SQL_TYPES.put("VARBINARY", "byte[]");

	}

	private Util() {

	}

	/**
	 * 根据表名生成类名, 表名格式 t_x_x_x_[reflect to Class]
	 * 
	 * @param tableName
	 * @return
	 */
	public static String getClassName(String tableName) {
		/* @TODO 包名类名的获取均需要重构 */
		StringBuffer result = new StringBuffer();
		String[] values = tableName.split("_");
		for (int i = 2; i < values.length; i++) {
			result.append(changeFirstToUpperCase(values[i]));
		}
		return result.toString();
	}

	/**
	 * 将首字母改为小写<br>
	 * 
	 * @param value
	 * @return
	 */
	public static String changeFirstToLowerCase(String value) {
		return value.substring(0, 1).toLowerCase()
				+ value.substring(1, value.length());
	}

	/**
	 * 将首字母改为大写<br>
	 * 
	 * @param value
	 * @return
	 */
	public static String changeFirstToUpperCase(String value) {
		return value.substring(0, 1).toUpperCase()
				+ value.substring(1, value.length());
	}

	/**
	 * 
	 * @param sqlType
	 * @return
	 */
	public static String getJavaTypeName(String sqlType) {
		return SQL_TYPES.get(sqlType);
	}

	/**
	 * @param javaType
	 * @return
	 */
	public static String getSqlTypeName(String javaType) {
		for (String sqlType : SQL_TYPES.keySet()) {
			if (SQL_TYPES.get(sqlType).equalsIgnoreCase(javaType)) {
				return sqlType;
			}
		}
		return "";
	}

	/**
	 * 删除最后的某个字符
	 * 
	 * @param delete
	 * @param content
	 */
	public static void deleteLastChar(String delete, StringBuffer content) {
		int index = content.lastIndexOf(delete);
		if (index != -1) {
			content.deleteCharAt(index);
		}
	}
}
